package driver.nxt.events;

import icommand.nxt.SensorPort;
import icommand.nxt.UltrasonicSensor;
import driver.events.Event;
import driver.exceptions.SensorException;

/**
 * Provides a mechanism for reading from the NXT Ultrasonic Sensor
 *
 * @author David Martin
 */
public class NXTEchoEvent extends Event
{
  private static final String EVENT_NAME = "Ultrasonic Sensor";
  private UltrasonicSensor sensor;

  /**
   * Select the NXT Sensor port for this sensor
   */
  public NXTEchoEvent()
  {
    super();
    sensor = new UltrasonicSensor(SensorPort.S2);
  }

  public String getEventName()
  {
    return EVENT_NAME;
  }

  /**
   * Gets the distance to the nearest object by polling the sensor
   *
   * @return the sensor reading as a string
   * @throws driversuite.exceptions.SensorException
   */
  public String readFromSensor() throws SensorException
  {
    return String.valueOf(sensor.getDistance());
  }

  public String getMeasurementUnits()
  {
    return "cm";
  }
}

